package com.xs.controller;

import java.net.URI;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xs.api.impl.OrderServiceApiImpl;
import com.xs.api.impl.UserServiceApiImpl;
import com.xs.entity.User;

@RestController
public class UserController {

	private Log log = LogFactory.getLog(UserController.class);

	@Autowired
	private RestTemplate template;

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private UserServiceApiImpl userService;

	@Autowired
	private OrderServiceApiImpl orderService;

	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@GetMapping("/getServices")
	public String dc() {
		String services = "Services: " + discoveryClient.getServices();
		log.info(services);
		return services;
	}

	@GetMapping("/hello-service")
	@HystrixCommand(fallbackMethod = "getFallback")
	public String helloService() {
		String services = "Services: " + discoveryClient.getServices();
		log.info(services);
		long currentTime = System.currentTimeMillis();
		if (currentTime % 2 == 0) {
			try {
				Thread.sleep(new Random(50).nextInt() * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return services;
	}

	public String getFallback() { // 此时方法的参数 // 与get()一致
		return "ERROR";
	}

	@RequestMapping(value = "/helloEx", method = RequestMethod.GET)
	public String helloEx() {
		ServiceInstance instance = this.loadBalancerClient.choose("USER-API");
		URI helloUri = URI
				.create(String.format("http://%s:%s/userApi/helloService", instance.getHost(), instance.getPort()));
		log.info("Target service uri = " + helloUri.toString());
		return new RestTemplate().getForEntity(helloUri, String.class).getBody();
	}

	@GetMapping("/getProvider")
	public String getProvider() {
		String result = template.postForObject("http://USER-API/userApi/helloService", null, String.class);
		return result;
	}

	@GetMapping("/helloService")
	public String helloService2() {
		String result = userService.helloService();
		return result;
	}

	@GetMapping("/getUserById")
	public User getUserById() {
		User user = userService.getUserById(new Random().nextInt(100));
		return user;
	}

	@PostMapping("/getUser")
	public User getUser(@RequestBody User u) {
		log.info("getUser():" + u);
		User user = userService.getUserInfo(u);
		return user;
	}

	@GetMapping("/getOrders")
	public String getOrder() {
		String result = orderService.getOrders();
		return result;
	}

}
